package com.ruoyi.business.controller.fuyou;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.ruoyi.business.service.FuiouNotifyService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.dto.PaymentDTO;
import com.ruoyi.common.core.domain.dto.RefundDTO;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ip.ClientIPUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;


/**
 * 富友支付
 *
 * @author ruoyi
 * @date 2024-04-15
 */
@RestController
@RequestMapping("wx")
@Slf4j
public class FuiouNotifyController {

    @Autowired
    private WxMaService wxMaService;

    @Autowired
    private FuiouNotifyService fuiouNotifyService;


    /**
     * 回调
     * @param params
     * @return
     */
    @PostMapping("/fuiou/notify")
    @Anonymous
    public String handleNotify(@RequestBody String notificationData) {
       return fuiouNotifyService.handleNotify(notificationData);
    }


    /**
     * 富友支付(预下单)
     * @param paymentDTO
     * @return
     * @throws Exception
     */
    @GetMapping("pay")
    @Anonymous
    public AjaxResult fuYouPay(PaymentDTO paymentDTO, HttpServletRequest request) throws Exception {
        WxMaJscode2SessionResult sessionInfo = wxMaService.getUserService().getSessionInfo(paymentDTO.getCode());
        String openid = sessionInfo.getOpenid();
        BigDecimal payAmount = paymentDTO.getOrderAmt().multiply(new BigDecimal(100));
        AjaxResult ajaxResult = fuiouNotifyService.fuYouPay(paymentDTO, payAmount,  openid);
       return ajaxResult;
    }


    /**
     * 处理退款请求
     * @param refundDTO 退款请求对象
     * @return 响应字符串
     */
    @PostMapping("/refund")
    @Anonymous
    public AjaxResult refund(@RequestBody RefundDTO refundDTO) {
        //String result = fuiouNotifyService.refund(refundDTO);
        String result = "";
        return AjaxResult.success(result);
    }
    /**
     * 处理退款请求
     * @param refundDTO 退款请求对象
     * @return 响应字符串
     */
    @PostMapping("/payment")
    @Anonymous
    public AjaxResult payment(RefundDTO refundDTO) {
        //String result = fuiouNotifyService.payment(refundDTO);
        String result = "";
        return AjaxResult.success(result);
    }

}
